using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Client;
using Ltf.Remoting.Settings;
using System.Runtime.InteropServices;
using System.Collections;
using Ltf.Remoting.RemoteEvents;
using Ltf.Remoting.TeamFoundation;

namespace Ltf.MonitorLizard.Forms
{

	[FormType(FormTypes.UndoPending)]
	public partial class UndoPending : Form
	{
		private int sortColumnIndex = 0;
		private bool sortDesc = false;
		public UndoPending(string[] pathList, LizardEventArgs args)
		{
			InitializeComponent();
			//lsvChanges.OwnerDraw = true;
			//		lsvChanges.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(lsvChanges_DrawColumnHeader);
			lsvChanges.ColumnClick += new ColumnClickEventHandler(lsvChanges_ColumnClick);
			LoadPendingList(pathList);
		}

		void lsvChanges_ColumnClick(object sender, ColumnClickEventArgs e)
		{
			//lsvChanges.Invalidate(new Rectangle(0,0,lsvChanges.Width,20),false);
			//lsvChanges.Refresh();
			lsvChanges.Columns[sortColumnIndex].ImageKey = null;
			if (sortColumnIndex == e.Column)
				sortDesc = !sortDesc;
			else
			{
				sortDesc = false;
				sortColumnIndex = e.Column;
			}
			if (sortDesc)
				lsvChanges.Columns[sortColumnIndex].ImageKey = "up";
			else
				lsvChanges.Columns[sortColumnIndex].ImageKey = "down";
			lsvChanges.ListViewItemSorter = (IComparer)new Sorter(sortColumnIndex, sortDesc);
			lsvChanges.Sort();
		}



		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void LoadPendingList(string[] pathList)
		{
			string firstPath = pathList[0];
			TFServerConnection connection = ServerConnections.GetConnectionForPath(firstPath);

			List<string> files = new List<string>(pathList);
			string fName = Path.GetDirectoryName(firstPath);
			VersionControlServer vcServer = connection.VersionControlServer;

			Workspace ws = connection.GetWorkspace(fName);

			lsvChanges.Items.Clear();
			PendingChange[] changes = ws.GetPendingChanges();
			//ImageList iml = new ImageList();
			//lsvChanges.SmallImageList = iml;
			foreach (PendingChange pc in changes)
			{
				ListViewItem lvi = lsvChanges.Items.Add(pc.FileName);
				lvi.Checked = (files.Contains(pc.LocalItem));
				imlHeader.Images.Add(pc.FileName, Ltf.MonitorLizard.Helpers.FileIcons.GetFileIcon(pc.FileName));
				lvi.ImageKey = pc.FileName;
				lvi.SubItems.Add(pc.ChangeTypeName);
				lvi.SubItems.Add(pc.ServerItem);
			}
		}

		

	}






	
}